#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Zhangcl

import paramiko

# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
try:
    ssh.connect(hostname='192.168.4.240', port=22, username='root', password='simple')
#   ssh.connect(hostname='192.168.3.74', port=22, username='root', password='simple')
    # 执行命令
    stdin, stdout, stderr = ssh.exec_command('df -h')
    # 获取命令结果
    result = stdout.read()
    print(result.decode())

    # 关闭连接
    ssh.close()
except TimeoutError as e:
    print(e)